<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\logic\AdminRoleLogic;
use think\facade\Request;

/**=========================================
 * Copyright © 2023-2024, WICMS.CN. All Rights Reserved
 * Project Name: WiCMS
 * Description: 角色控制器
 * Author: jason
 * Email: 52527998@QQ.COM
 * Date: 2024/10/12
 * Time: 下午5:03
 * File: app\admin\controller\Roles.php
 * =========================================
 */
class Role extends BaseModule
{

    public function initialize(): void
    {
        parent::initialize();
    }

    public function get_list(): void
    {
        $condition = [];
        $isPage = Request::post('is_page/d');
        $role_name = Request::post('role_name/s', '');
        $role_code = Request::post('role_code/s', '');
        $status = Request::post('status/s', '');
        if (!empty($role_name)) {
            $condition[] = ['role_name', 'like', '%' . $role_name . '%'];
        }
        if (!empty($role_code)) {
            $condition[] = ['role_code', 'like', '%' . $role_code . '%'];
        }
        if ($status != '') {
            $condition[] = ['status', '=', $status];
        }
        $list      = AdminRoleLogic::getList($condition, $isPage == 1);
        $this->success('获取成功', $list);
    }

    public function get_role_menu_ids(): void
    {
        $role_id = input('get.id/d', 0,'intval');
        $menu_ids = AdminRoleLogic::getRoleMenuIds($role_id);
        $this->success('success',$menu_ids);
    }

    public function save_data():void
    {
        $data = Request::post('');
        $result = AdminRoleLogic::saveData($data);
        if ($result === true) {
            $this->success('success');
        } else {
            $this->error($result);
        }
    }

    public function del_data():void
    {
        $id = Request::get('id/d', 0);
        if (empty($id)) {
            $this->error('参数错误');
        }
        $result = AdminRoleLogic::deleteData($id);
        if ($result !== true) {
            $this->error($result);
        } else {
            $this->success('success');
        }
    }

    public function set_role_allow_auth():void
    {
        $role_id = Request::post('role_id/d', 0);
        $allow_auths = Request::post('allow_auths/a', []);
        if (empty($role_id)) {
            $this->error('参数错误');
        }
        $result = AdminRoleLogic::setRoleAllowAuth($role_id, $allow_auths);
        if ($result !== true) {
            $this->error($result);
        } else {
            $this->success('success');
        }
    }

    public function set_role_status():void
    {
        $role_id = Request::post('role_id/d', 0);
        $status = Request::post('status/d', 0);
        if (empty($role_id)) {
            $this->error('参数错误');
        }
        $result = AdminRoleLogic::setRoleStatus($role_id, $status);
        if ($result !== true) {
            $this->error($result);
        } else {
            $this->success('success');
        }
    }
}